<template>
	<div>


		<van-cell title="入库单" is-link value="正在进行的单据" class="cell_header_title" @click="toOutOrInWarehose" />

		<ChangeWarehouse v-on:listenToChangeWarehouseEvent="changeWarehouseId"></ChangeWarehouse>
		<!-- <van-cell title="往来单位" is-link value="选择往来单位" /> -->
		<ChangeSubject v-on:listenToChangeSubjectEvent="changeSubjectId"></ChangeSubject>

		<van-cell title="备注" class="cell_header_title" />
		<van-field v-model="remark" label="备注" placeholder="请输入" />

		<AddGoods v-bind:warehouse-id='warehouse_id' v-on:listenToExitAddGoodsEvent="renderGoods"></AddGoods>


		<!-- 商品渲染 -->
		<van-empty v-if="this.sku_list.length==0" description="没有对应的SKU" />
		<van-swipe-cell v-else v-for="item in sku_list">
			<van-card :tag="item.have_chosen?'选择':''" :num="item.selected_quantity?item.selected_quantity:null"
				:desc="item.attribute_str" :price="item.price" :title="item.brand_name + item.spu_name"
				thumb="https://img01.yzcdn.cn/vant/ipad.jpeg">

				<template #tags>
					<van-tag plain type="danger">货位:{{item.location_name}}</van-tag>
					<!-- <van-tag plain type="danger">标签</van-tag> -->
				</template>

			</van-card>
			<template #right>
				<van-button square text="删除" type="danger" class="delete-button" @click="delete_item(item)" />
			</template>
		</van-swipe-cell>




		<div v-show="permission_operator">
			<van-button v-on:click="put_in_storage" 
			type="primary" style="width: 100%;">入库 (产品种类:{{ sku_list.length }})
			</van-button>
		</div>

		<div style="margin-top: 60px;"></div>
	</div>
</template>

<script>
	import Cookies from 'js-cookie'
	import {
		Toast
	} from 'vant'
	
	import {
			get_duty_warehouse_ship
		} from '../../utils/get_duty_ship.js'

	import {
		add_odd_url,
		add_sku_account_in_url
	} from "../../utils/setting.js"
	// import ChangeSubject from "./change_subject.vue"
	// import ChangeWarehouse from "./change_warehouse.vue"
	import ChangeWarehouse from "../change_item/change_warehouse.vue"
	import ChangeSubject from "../change_item/change_subject.vue"
	import AddGoods from "./add_goods.vue"
	export default {
		name: '',
		components: {
			ChangeWarehouse,
			ChangeSubject,
			AddGoods
		},
		props: {},
		data() {
			return {

				remark: '',
				warehouse_id: null,
				subject_id: '',
				sku_list: [],
				permission_operator: false,

			}
		},
		methods: {

			toOutOrInWarehose() {
				this.$router.push({
					'name': 'OutOrInWarehose',
					query: {status: 'wait'}
				})
			},

			delete_item(item) {
				console.log(item)

				for (let i = 0; i < this.sku_list.length; i++) {

					// console.log(this.sku_list[i].id)
					if (this.sku_list[i].id == item.id) {
						item.have_chosen = false
						delete item.selected_quantity
						this.sku_list.splice(i, 1)
					}
				}

				// console.log(this.sku_list)
			},

			changeWarehouseId(data) {
				// console.log(data)
				this.permission_operator = false
				this.warehouse_id = data['warehouse_id']
				// console.log(this.warehouse_id)
				get_duty_warehouse_ship(
					(res) => {
						console.log(res)
				
						for (let i = 0; i < res.warehouse_list.length; i++) {
							let val = res.warehouse_list[i]
				
							console.log(val)
							if (val.in_ == true && val.warehouse_id == this.warehouse_id) {
								this.permission_operator = true
							}
						}
					}
				)
			},

			changeSubjectId(data) {
				// console.log(data)
				this.subject_id = data['subject_id']
				// console.log(this.subject_id)
			},

			renderGoods(data) {
				console.log(data)
				this.sku_list = data
			},




			//添加定单
			put_in_storage() {
				if (this.warehouse_id == null) {
					Toast('请选择库房')
					return
				}
				if (this.subject_id == '') {
					Toast('请选择往来单位')
					return
				}
				if (this.remark == '') {
					Toast('请填写备注，库房和往来单位不填写默认第一位')
					return
				}
				if (this.sku_list.length == 0) {
					Toast('请选择产品')
					return
				}
				this.$axios({
					method: 'POST',
					url: add_odd_url,
					data: this.qs.stringify({ //这里是发送给后台的数据
						in_or_out: true, //入库单
						subject_id: this.subject_id,
						sponsor_id: Cookies.get('user_id'),
						warehouse_id: this.warehouse_id,
						remark: this.remark
						// password: password,
					})
				}).then((response) => { //这里使用了ES6的语法
					// console.log(response) //请求成功返回的数据

					let odd_id = response.data.resultBody['id']
					//商品依次添加到订单
					for (let i = 0; i < this.sku_list.length; i++) {
						this.add_sku_account_in_odd(odd_id, this.sku_list[i])
					}

					this.remark = ''

					Toast('订单添加成功')

					setTimeout(function() {

							location.reload()
						}, 1000

					)
				}).catch((error) => {
					// console.log(error) //请求失败返回的数据
					this.$message({
						message: '请联系管理员' + '!!!',
						type: 'error'
					})
				})
			},

			//定单中添加产品 add_sku_account_in

			add_sku_account_in_odd(odd_id, account_info) {
				this.$axios({
					method: 'POST',
					url: add_sku_account_in_url,
					data: this.qs.stringify({ //这里是发送给后台的数据
						odd_id: odd_id, //入库单
						sku_id: account_info.id,
						storage_location_id: account_info.location_id,
						quantity: account_info.selected_quantity,
						price: account_info.price
						// password: password,
					})
				}).then((response) => { //这里使用了ES6的语法
					console.log(response) //请求成功返回的数据


				}).catch((error) => {
					// console.log(error) //请求失败返回的数据
					this.$message({
						message: '请联系管理员' + '!!!',
						type: 'error'
					})
				})
			}


		},
		computed: {},
		watch: {},
		created() {},
		mounted() {},
	}
</script>

<style scoped lang="less">
	.cell_header_title {
		background-color: #F0F0F0;
		font-weight: 800;
	}

	.delete-button {
		height: 100%;
	}
</style>
